<?php
/**
 * Created by PhpStorm.
 * User: 65736
 * Date: 2019/3/6 0006
 * Time: 17:58
 */

namespace app\common\widget;


use app\common\library\CatchException;
use app\common\library\CommonFun;
use Exception;

class WidgetSelect extends WidgetInput
{

    /**
     * select 配置参数
     * @var array
     */
    var $config = array();

    /**
     * 是否多选
     * @var bool
     */
    var $isMuti = false;

    /**
     * 是否支持搜索
     * @var bool
     */
    var $isSearch = false;

    /**
     * Options 数组
     * @var array
     */
    var $options = [];

    /**
     * Select value 值
     * @var string
     */
    var $value = '';

    /**
     * 渲染挂件
     * @return mixed
     * @throws \think\Exception
     * @throws Exception
     */
    function render()
    {
        // 检查ID
        $id = $this->getId();
        if ( !$id ) throw new Exception("请设置表单元素ID", CatchException::WidgetErrorException);
        // 分配元素列表
        if( !$this->options ) throw new Exception("Select挂件没有Options元素", CatchException::WidgetErrorException);
        self::$view->assign('options', $this->options);
        self::$view->assign('value', $this->value);
        self::$view->assign('is_search', $this->isSearch);
        self::$view->assign('is_muti', $this->isMuti);
        self::$view->assign('config', CommonFun::json_encode2($this->config));
        return $this->fetch();
    }

    /**
     * 初始化方法
     * @throws \think\Exception
     * @throws Exception
     */
    protected function initialize()
    {
        parent::initialize();
        $this->setClass('select2');
        $this->setDomAttr('tabindex', '-1');
        $this->setDomAttr('aria-hidden', 'true');
        // 默认关闭搜索
        $this->setConfig('minimumResultsForSearch', 'Infinity');
    }


    /**
     * 设置SelectConfig配置
     * @param $name
     * @param $value
     * @return $this
     * @throws Exception
     */
    public function setConfig( $name, $value ) {
        if ( !$name ) throw new Exception( "设置SelectConfig配置参数错误", CatchException::WidgetErrorException );
        $this->config[$name] = $value;
        return $this;
    }

    /**
     * 设置value值
     * @param $value
     * @return $this|WidgetInput
     * @throws Exception
     */
    public function setValue($value)
    {
        if ( !$value ) throw new Exception( "select value 值非法", CatchException::WidgetErrorException );
        if ( is_array($value) ) $value = CommonFun::json_encode2($value);
        $this->value = $value;
        return $this;
    }

    /**
     * 获取value值
     * @return mixed|null|string
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * 设置Option列表
     * @param array $options
     * @return $this
     * @throws Exception
     */
    public function setOptions( array $options ) {
        if( !$options ) throw new Exception("Options列表数据非法", CatchException::WidgetErrorException);
        $this->options = $options;
        $data = [];
        foreach ( $options as $key=>$v ) {
            $data[] = array(
                'id' => $key,
                'text' => $v,
            );
        }
        $this->setConfig('data', $data);
        return $this;
    }

    /**
     * 设置多选
     * @return $this
     * @throws Exception
     */
    public function setMuti() {
        $this->isMuti = true;
        $this->setConfig('multiple', true);
        return $this;
    }

    /**
     * 设置搜索
     * @return $this
     * @throws Exception
     */
    public function setSearch() {
        $this->isSearch = true;
        $this->setConfig('minimumResultsForSearch', 0);
        return $this;
    }

    /**
     * 设置placeholder值
     * @param $value
     * @return $this|WidgetInput
     * @throws Exception
     */
    public function setPlaceholder($value)
    {
        $this->setConfig('placeholder', $value);
        return $this;
    }


}